package contract_consumer;

import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
@AutoConfigureStubRunner(ids = {"com.example:contract_provider:0.0.1-SNAPSHOT:stubs:10001"},stubsMode = StubRunnerProperties.StubsMode.LOCAL)
//@ActiveProfiles("stub")
@Slf4j
public class ContractConsumerTest {

//    private MockMvc mockMvc;

//    @Autowired
//    private WebApplicationContext webApplicationContext;

//    @Autowired
//    private CustomerConsumerAction customerConsumerAction;
//
//    @Before
//    public void setUp() {
//        mockMvc = MockMvcBuilders.standaloneSetup(customerConsumerAction).build();
//    }
//
//    @Test
//    public void testGetCustomers1() throws Exception {
//        mockMvc.perform(MockMvcRequestBuilders.get("/cus")).andExpect(MockMvcResultMatchers.status().isOk())
//                .andDo(MockMvcResultHandlers.print());
//
//    }

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void testMethod() throws Exception {
        ResponseEntity<JSONObject> response = restTemplate.exchange(
                "http://localhost:10001/api/customers",
                HttpMethod.GET,
                null,
                JSONObject.class
        );
        log.info("测试数据:"+ response.getBody().toJSONString());
    }
}